﻿using System;
using System.Text;
using jQueryCSharp.Extensions;

namespace jQueryCSharp.ui
{
    public class Datepicker : jQueryCSharpBase
    {
        #region Constantes
        private const bool DISABLED = false;
        private const string ALTFIELD = "";
        private const string ALTFORMAT = "";
        private const string APPENDTEXT = "";
        private const bool AUTOSIZE = false;
        private const string BUTTONIMAGE = "";
        private const bool BUTTONIMAGEONLY = false;
        private const string BUTTONTEXT = "...";
        private const string CALCULATEWEEK = "$.datepicker.iso8601Week";
        private const bool CHANGEMONTH = false;
        private const bool CHANGEYEAR = false;
        private const string CLOSETEXT = "Done";
        private const bool CONSTRAININPUT = true;
        private const string CURRENTTEXT = "Today";
        private const string DATEFORMAT = "mm/dd/yy";
        private const string DAYNAMES = "['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']";
        private const string DAYNAMESMIN = "['Su','Mo','Tu','We','Th','Fr','Sa']";
        private const string DAYNAMESSHORT = "['Sun','Mon','Tue','Wed','Thu','Fri','Sat']";
        private const string DURATION = "'normal'";
        private const int FIRSTDAY = 0;
        private const bool GOTOCURRENT = false;
        private const bool HIDEIFNOPREVNEXT = false;
        private const bool ISRTL = false;
        private const string MONTHNAMES = "['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']";
        private const string MONTHNAMESSHORT = "['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']";
        private const bool NAVIGATIONASDATEFORMAT = false;
        private const string NEXTTEXT = "NEXT";
        private const int NUMBEROFMONTHS = 1;
        private const string PREVTEXT = "PREV";
        private const bool SELECTOTHERMONTHS = false;
        private const int SHORTYEARCUTOFF = 10;
        private const string SHOWANIM = "show";
        private const bool SHOWBUTTONPANEL = false;
        private const int SHOWCURRENTATPOS = 0;
        private const bool SHOWMONTHAFTERYEAR = false;
        private const string SHOWON = "focus";
        private const bool SHOWOTHERMONTHS = false;
        private const bool SHOWWEEK = false;
        private const int STEPMONTHS = 1;
        private const string WEEKHEADER = "WK";
        private const string YEARRANGE = "'c-10:c+10'";
        private const string YEARSUFFIX = "";

        private readonly string DEFAULTDATE = new DateTime( 1900, 01, 01 ).ToString( );
        private readonly DateTime MAXDATE = new DateTime( 2075, 12, 31 );
        private readonly DateTime MINDATE = new DateTime( 1900, 01, 01 );

        #endregion

        /// <summary>
        /// Construtor
        /// </summary>
        public Datepicker( )
            : this( "" )
        {

        }

        /// <summary>
        /// Construtor
        /// </summary>
        public Datepicker( string identifier )
        {
            Disabled = DISABLED;
            AltField = ALTFIELD;
            AltFormat = ALTFORMAT;
            AppendText = APPENDTEXT;
            AutoSize = AUTOSIZE;
            ButtonImage = BUTTONIMAGE;
            ButtonImageOnly = BUTTONIMAGEONLY;
            ButtonText = BUTTONTEXT;
            CalculateWeek = CALCULATEWEEK;
            ChangeMonth = CHANGEMONTH;
            ChangeYear = CHANGEYEAR;
            CloseText = CLOSETEXT;
            ConstrainInput = CONSTRAININPUT;
            CurrentText = CURRENTTEXT;
            DateFormat = DATEFORMAT;
            DayNames = DAYNAMES;
            DayNamesMin = DAYNAMESMIN;
            DayNamesShort = DAYNAMESSHORT;
            DefaultDate = DEFAULTDATE;
            Duration = DURATION;
            FirstDay = FIRSTDAY;
            GoToCurrent = GOTOCURRENT;
            HideIfNoPrevNext = HIDEIFNOPREVNEXT;
            IsRTL = ISRTL;
            MaxDate = MAXDATE;
            MinDate = MINDATE;
            MonthNames = MONTHNAMES;
            MonthNamesShort = MONTHNAMESSHORT;
            NavigationAsDateFormat = NAVIGATIONASDATEFORMAT;
            NextText = NEXTTEXT;
            NumberOfMonths = NUMBEROFMONTHS;
            PrevText = PREVTEXT;
            SelectOtherMonths = SELECTOTHERMONTHS;
            ShortYearCutoff = SHORTYEARCUTOFF;
            ShowAnim = SHOWANIM;
            ShowButtonPanel = SHOWBUTTONPANEL;
            ShowCurrentAtPos = SHOWCURRENTATPOS;
            ShowMonthAfterYear = SHOWMONTHAFTERYEAR;
            ShowOn = SHOWON;
            ShowOtherMonths = SHOWOTHERMONTHS;
            ShowWeek = SHOWWEEK;
            StepMonths = STEPMONTHS;
            WeekHeader = WEEKHEADER;
            YearRange = YEARRANGE;
            YearSuffix = YEARSUFFIX;

            if ( !String.IsNullOrEmpty( identifier ) )
            {
                if ( identifier.Contains( "#" ) )
                {
                    ID = identifier.Replace( "#", "" );
                }
                else
                {
                    Class = identifier.Replace( ".", "" );
                }
            }
        }

        #region Propriedades
        public bool Disabled { get; set; }
        public string AltField { get; set; }
        public string AltFormat { get; set; }
        public string AppendText { get; set; }
        public bool AutoSize { get; set; }
        public string ButtonImage { get; set; }
        public bool ButtonImageOnly { get; set; }
        public string ButtonText { get; set; }
        public string CalculateWeek { get; set; }
        public bool ChangeMonth { get; set; }
        public bool ChangeYear { get; set; }
        public string CloseText { get; set; }
        public bool ConstrainInput { get; set; }
        public string CurrentText { get; set; }
        public string DateFormat { get; set; }
        public string DayNames { get; set; }
        public string DayNamesMin { get; set; }
        public string DayNamesShort { get; set; }
        public string DefaultDate { get; set; }
        public string Duration { get; set; }
        public int FirstDay { get; set; }
        public bool GoToCurrent { get; set; }
        public bool HideIfNoPrevNext { get; set; }
        public bool IsRTL { get; set; }
        public DateTime MaxDate { get; set; }
        public DateTime MinDate { get; set; }
        public string MonthNames { get; set; }
        public string MonthNamesShort { get; set; }
        public bool NavigationAsDateFormat { get; set; }
        public string NextText { get; set; }
        public int NumberOfMonths { get; set; }
        public string PrevText { get; set; }
        public bool SelectOtherMonths { get; set; }
        public int ShortYearCutoff { get; set; }
        public string ShowAnim { get; set; }
        public bool ShowButtonPanel { get; set; }
        public int ShowCurrentAtPos { get; set; }
        public bool ShowMonthAfterYear { get; set; }
        public string ShowOn { get; set; }
        public bool ShowOtherMonths { get; set; }
        public bool ShowWeek { get; set; }
        public int StepMonths { get; set; }
        public string WeekHeader { get; set; }
        public string YearRange { get; set; }
        public string YearSuffix { get; set; }

        #endregion

        public override string ToString( )
        {
            var sb = new StringBuilder( );


            if ( Disabled != DISABLED )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "disabled", Disabled.ToString( ).ToLower( ) ) );
            }

            if ( AltField != ALTFIELD )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "altField", "'{0}'".Formatar( AltField ) ) );
            }

            if ( AltFormat != ALTFORMAT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "altFormat", "'{0}'".Formatar( AltFormat ) ) );
            }

            if ( AppendText != APPENDTEXT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "appendText", "'{0}'".Formatar( AppendText ) ) );
            }

            if ( AutoSize != AUTOSIZE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "autoSize", AutoSize.ToString( ).ToLower( ) ) );
            }

            if ( ButtonImage != BUTTONIMAGE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "buttonImage", "'{0}'".Formatar( ButtonImage ) ) );
            }

            if ( ButtonImageOnly != BUTTONIMAGEONLY )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "buttonImageOnly", ButtonImageOnly.ToString( ).ToLower( ) ) );
            }

            if ( ButtonText != BUTTONTEXT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "buttonText", "'{0}'".Formatar( ButtonText ) ) );
            }

            if ( CalculateWeek != CALCULATEWEEK )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "calculateWeek", "'{0}'".Formatar( CalculateWeek ) ) );
            }

            if ( ChangeMonth != CHANGEMONTH )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "changeMonth", ChangeMonth.ToString( ).ToLower( ) ) );
            }

            if ( ChangeYear != CHANGEYEAR )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "changeYear", ChangeYear.ToString( ).ToLower( ) ) );
            }

            if ( CloseText != CLOSETEXT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "closeText", "'{0}'".Formatar( CloseText ) ) );
            }

            if ( ConstrainInput != CONSTRAININPUT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "constrainInput", ConstrainInput.ToString( ).ToLower( ) ) );
            }

            if ( CurrentText != CURRENTTEXT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "currentText", "'{0}'".Formatar( CurrentText ) ) );
            }

            if ( DateFormat != DATEFORMAT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "dateFormat", "'{0}'".Formatar( DateFormat ) ) );
            }

            if ( DayNames != DAYNAMES )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "dayNames", "'{0}'".Formatar( DayNames ) ) );
            }

            if ( DayNamesMin != DAYNAMESMIN )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "dayNamesMin", "'{0}'".Formatar( DayNamesMin ) ) );
            }

            if ( DayNamesShort != DAYNAMESSHORT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "dayNamesShort", "'{0}'".Formatar( DayNamesShort ) ) );
            }

            if ( DefaultDate != DEFAULTDATE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "defaultDate", "'{0}'".Formatar( DefaultDate ) ) );
            }

            if ( Duration != DURATION )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "duration", "'{0}'".Formatar( Duration ) ) );
            }

            if ( FirstDay != FIRSTDAY )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "firstDay", FirstDay.ToString( ) ) );
            }

            if ( GoToCurrent != GOTOCURRENT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "gotoCurrent", GoToCurrent.ToString( ).ToLower( ) ) );
            }

            if ( HideIfNoPrevNext != HIDEIFNOPREVNEXT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "hideIfNoPrevNext", HideIfNoPrevNext.ToString( ).ToLower( ) ) );
            }

            if ( IsRTL != ISRTL )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "isRTL", IsRTL.ToString( ).ToLower( ) ) );
            }

            if ( MaxDate != MAXDATE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "maxDate", MaxDate.ToString( ) ) );
            }

            if ( MinDate != MINDATE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "minDate", MinDate.ToString( ) ) );
            }

            if ( MonthNames != MONTHNAMES )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "monthNames", "'{0}'".Formatar( MonthNames ) ) );
            }

            if ( MonthNamesShort != MONTHNAMESSHORT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "monthNamesShort", "'{0}'".Formatar( MonthNamesShort ) ) );
            }

            if ( NavigationAsDateFormat != NAVIGATIONASDATEFORMAT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "navigationAsDateFormat", NavigationAsDateFormat.ToString( ).ToLower( ) ) );
            }

            if ( NextText != NEXTTEXT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "nextText", "'{0}'".Formatar( NextText ) ) );
            }

            if ( NumberOfMonths != NUMBEROFMONTHS )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "numberOfMonths", "'{0}'".Formatar( NumberOfMonths ) ) );
            }

            if ( PrevText != PREVTEXT )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "prevText", "'{0}'".Formatar( PrevText ) ) );
            }

            if ( SelectOtherMonths != SELECTOTHERMONTHS )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "selectOtherMonths", SelectOtherMonths.ToString( ).ToLower( ) ) );
            }

            if ( ShortYearCutoff != SHORTYEARCUTOFF )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "shortYearCutoff", ShortYearCutoff.ToString( ) ) );
            }

            if ( ShowAnim != SHOWANIM )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "showAnim", "'{0}'".Formatar( ShowAnim ) ) );
            }

            if ( ShowButtonPanel != SHOWBUTTONPANEL )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "showButtonPanel", ShowButtonPanel.ToString( ).ToLower( ) ) );
            }

            if ( ShowCurrentAtPos != SHOWCURRENTATPOS )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "showCurrentAtPos", ShowCurrentAtPos.ToString( ) ) );
            }

            if ( ShowMonthAfterYear != SHOWMONTHAFTERYEAR )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "showMonthAfterYear", ShowMonthAfterYear.ToString( ).ToLower( ) ) );
            }

            if ( ShowOn != SHOWON )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "showOn", "'{0}'".Formatar( ShowOn ) ) );
            }

            if ( ShowOtherMonths != SHOWOTHERMONTHS )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "showOtherMonths", ShowOtherMonths.ToString( ).ToLower( ) ) );
            }

            if ( ShowWeek != SHOWWEEK )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "showWeek", ShowWeek.ToString( ).ToLower( ) ) );
            }

            if ( StepMonths != STEPMONTHS )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "stepMonths", StepMonths.ToString( ) ) );
            }

            if ( WeekHeader != WEEKHEADER )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "weekHeader", "'{0}'".Formatar( WeekHeader ) ) );
            }

            if ( YearRange != YEARRANGE )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "yearRange", "'{0}'".Formatar( YearRange ) ) );
            }

            if ( YearSuffix != YEARSUFFIX )
            {
                if ( sb.Length > 0 ) sb.Append( "," );
                sb.Append( modelo.Formatar( "yearSuffix", "'{0}'".Formatar( YearSuffix ) ) );
            }


            if ( sb.Length > 0 )
            {
                return String.Concat( "{", sb.ToString( ), "}" );
            }

            return sb.ToString( );
        }
    }
}
